<template>
  <div class="management-container">
    <!-- 顶部导航栏 -->
    <div class="management-header">
      <div class="logo">云POS</div>
      <div class="store-info">陕西临潼西大街人本专卖店 <span class="store-code">029388</span></div>
      <div class="header-actions">
        <el-button @click="goToPos">收银</el-button>
        <el-button type="primary" plain>店务管理</el-button>
        <el-button @click="goToInventory">仓储管理</el-button>
        <el-button @click="goToNav">店长导航</el-button>
        <el-button @click="goToReport">报表</el-button>
        <div class="header-icons">
          <el-icon><CircleCheck /></el-icon>
          <el-icon><Monitor /></el-icon>
          <el-icon><Right /></el-icon>
        </div>
      </div>
    </div>

    <!-- 主内容区 -->
    <div class="management-content">
      <el-row :gutter="20">
        <!-- 左侧菜单 -->
        <el-col :span="4">
          <el-menu
            class="management-menu"
            :default-active="activeMenu"
            @select="handleMenuSelect">
            <el-menu-item index="staff">
              <el-icon><User /></el-icon>
              <span>员工管理</span>
            </el-menu-item>
            <el-menu-item index="member">
              <el-icon><Stamp /></el-icon>
              <span>会员管理</span>
            </el-menu-item>
            <el-menu-item index="product">
              <el-icon><Goods /></el-icon>
              <span>商品管理</span>
            </el-menu-item>
            <el-menu-item index="discount">
              <el-icon><Discount /></el-icon>
              <span>促销管理</span>
            </el-menu-item>
            <el-menu-item index="order">
              <el-icon><Document /></el-icon>
              <span>订单管理</span>
            </el-menu-item>
            <el-menu-item index="shift">
              <el-icon><Timer /></el-icon>
              <span>交接班管理</span>
            </el-menu-item>
            <el-menu-item index="settings">
              <el-icon><Setting /></el-icon>
              <span>系统设置</span>
            </el-menu-item>
          </el-menu>
        </el-col>
        
        <!-- 右侧内容 -->
        <el-col :span="20">
          <div class="content-area">
            <!-- 员工管理 -->
            <div v-if="activeMenu === 'staff'" class="staff-management">
              <h2>员工管理</h2>
              <el-button type="primary" @click="showAddStaffDialog = true">新增员工</el-button>
              
              <el-table :data="staffList" style="width: 100%; margin-top: 20px" border>
                <el-table-column prop="id" label="工号" width="80"></el-table-column>
                <el-table-column prop="name" label="姓名" width="120"></el-table-column>
                <el-table-column prop="role" label="职位" width="120"></el-table-column>
                <el-table-column prop="phone" label="手机号" width="150"></el-table-column>
                <el-table-column prop="entryDate" label="入职日期" width="150"></el-table-column>
                <el-table-column prop="status" label="状态" width="100">
                  <template #default="scope">
                    <el-tag :type="scope.row.status === '在职' ? 'success' : 'danger'">
                      {{ scope.row.status }}
                    </el-tag>
                  </template>
                </el-table-column>
                <el-table-column label="操作" width="200">
                  <template #default="scope">
                    <el-button size="small" @click="editStaff(scope.row)">编辑</el-button>
                    <el-button 
                      size="small" 
                      type="danger" 
                      @click="confirmDeleteStaff(scope.row)">
                      删除
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
            </div>
            
            <!-- 会员管理 -->
            <div v-if="activeMenu === 'member'" class="member-management">
              <h2>会员管理</h2>
              <el-button type="primary" @click="showAddMemberDialog = true">新增会员</el-button>
              
              <el-table :data="memberList" style="width: 100%; margin-top: 20px" border>
                <el-table-column prop="id" label="会员号" width="120"></el-table-column>
                <el-table-column prop="name" label="姓名" width="120"></el-table-column>
                <el-table-column prop="phone" label="手机号" width="150"></el-table-column>
                <el-table-column prop="level" label="会员等级" width="120">
                  <template #default="scope">
                    <el-tag :type="getMemberLevelType(scope.row.level)">
                      {{ scope.row.level }}
                    </el-tag>
                  </template>
                </el-table-column>
                <el-table-column prop="points" label="积分" width="100"></el-table-column>
                <el-table-column prop="joinDate" label="注册日期" width="150"></el-table-column>
                <el-table-column prop="lastVisit" label="最近消费" width="150"></el-table-column>
                <el-table-column label="操作" width="200">
                  <template #default="scope">
                    <el-button size="small" @click="editMember(scope.row)">编辑</el-button>
                    <el-button 
                      size="small" 
                      type="primary" 
                      @click="viewMemberDetails(scope.row)">
                      详情
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
            </div>
            
            <!-- 默认显示概览 -->
            <div v-if="activeMenu === ''" class="dashboard">
              <h2>店务管理</h2>
              <el-row :gutter="20" class="stat-cards">
                <el-col :span="6">
                  <el-card class="stat-card">
                    <template #header>
                      <div class="card-header">
                        <span>今日销售</span>
                        <el-icon><Money /></el-icon>
                      </div>
                    </template>
                    <div class="stat-value">¥ 8,745.00</div>
                    <div class="stat-compare">较昨日 <span class="up">+12.5%</span></div>
                  </el-card>
                </el-col>
                <el-col :span="6">
                  <el-card class="stat-card">
                    <template #header>
                      <div class="card-header">
                        <span>今日客流</span>
                        <el-icon><User /></el-icon>
                      </div>
                    </template>
                    <div class="stat-value">124</div>
                    <div class="stat-compare">较昨日 <span class="up">+8.7%</span></div>
                  </el-card>
                </el-col>
                <el-col :span="6">
                  <el-card class="stat-card">
                    <template #header>
                      <div class="card-header">
                        <span>新增会员</span>
                        <el-icon><UserFilled /></el-icon>
                      </div>
                    </template>
                    <div class="stat-value">8</div>
                    <div class="stat-compare">较昨日 <span class="down">-2.5%</span></div>
                  </el-card>
                </el-col>
                <el-col :span="6">
                  <el-card class="stat-card">
                    <template #header>
                      <div class="card-header">
                        <span>平均客单价</span>
                        <el-icon><ShoppingCart /></el-icon>
                      </div>
                    </template>
                    <div class="stat-value">¥ 238.50</div>
                    <div class="stat-compare">较昨日 <span class="up">+5.2%</span></div>
                  </el-card>
                </el-col>
              </el-row>
              
              <div class="recent-data">
                <el-row :gutter="20">
                  <el-col :span="12">
                    <el-card class="chart-card">
                      <template #header>
                        <div class="card-header">
                          <span>销售趋势</span>
                        </div>
                      </template>
                      <div class="chart-placeholder">销售趋势图表</div>
                    </el-card>
                  </el-col>
                  <el-col :span="12">
                    <el-card class="recent-orders">
                      <template #header>
                        <div class="card-header">
                          <span>最近订单</span>
                        </div>
                      </template>
                      <el-table :data="recentOrders" style="width: 100%" :show-header="false">
                        <el-table-column width="60">
                          <template #default="scope">
                            <el-avatar :size="40" :src="scope.row.avatar"></el-avatar>
                          </template>
                        </el-table-column>
                        <el-table-column>
                          <template #default="scope">
                            <div class="order-info">
                              <div class="order-title">{{ scope.row.name }} - {{ scope.row.orderId }}</div>
                              <div class="order-meta">{{ scope.row.time }} · {{ scope.row.items }}件商品</div>
                            </div>
                          </template>
                        </el-table-column>
                        <el-table-column width="100" prop="amount" align="right">
                          <template #default="scope">
                            <div class="order-amount">¥{{ scope.row.amount }}</div>
                          </template>
                        </el-table-column>
                      </el-table>
                    </el-card>
                  </el-col>
                </el-row>
              </div>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
  
  <!-- 员工新增/编辑对话框 -->
  <el-dialog v-model="showAddStaffDialog" :title="staffDialogTitle" width="30%">
    <el-form :model="staffForm" label-width="80px">
      <el-form-item label="姓名">
        <el-input v-model="staffForm.name"></el-input>
      </el-form-item>
      <el-form-item label="职位">
        <el-select v-model="staffForm.role" placeholder="请选择">
          <el-option label="店长" value="店长"></el-option>
          <el-option label="营业员" value="营业员"></el-option>
          <el-option label="仓管" value="仓管"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="staffForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="入职日期">
        <el-date-picker v-model="staffForm.entryDate" type="date" placeholder="选择日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="staffForm.status" placeholder="请选择">
          <el-option label="在职" value="在职"></el-option>
          <el-option label="离职" value="离职"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="showAddStaffDialog = false">取消</el-button>
        <el-button type="primary" @click="saveStaff">确定</el-button>
      </div>
    </template>
  </el-dialog>
  
  <!-- 会员新增/编辑对话框 -->
  <el-dialog v-model="showAddMemberDialog" :title="memberDialogTitle" width="30%">
    <el-form :model="memberForm" label-width="80px">
      <el-form-item label="姓名">
        <el-input v-model="memberForm.name"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="memberForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="会员等级">
        <el-select v-model="memberForm.level" placeholder="请选择">
          <el-option label="普通会员" value="普通会员"></el-option>
          <el-option label="银卡会员" value="银卡会员"></el-option>
          <el-option label="金卡会员" value="金卡会员"></el-option>
          <el-option label="钻石会员" value="钻石会员"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="积分">
        <el-input-number v-model="memberForm.points" :min="0"></el-input-number>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="showAddMemberDialog = false">取消</el-button>
        <el-button type="primary" @click="saveMember">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { 
  CircleCheck, Monitor, Right, User, Stamp, Goods, 
  Discount, Document, Timer, Setting, Money, UserFilled,
  ShoppingCart
} from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const router = useRouter()
definePageMeta({
  layout: false
})
// 导航到其他页面
const goToPos = () => router.push('/pos')
const goToInventory = () => router.push('/inventory')
const goToNav = () => router.push('/manager-nav')
const goToReport = () => router.push('/report')

// 当前选中的菜单
const activeMenu = ref('')

// 处理菜单选择
const handleMenuSelect = (index) => {
  activeMenu.value = index
}

// 员工数据
const staffList = ref([
  { id: '001', name: '张三', role: '店长', phone: '13800138000', entryDate: '2022-01-15', status: '在职' },
  { id: '002', name: '李四', role: '营业员', phone: '13900139000', entryDate: '2022-03-20', status: '在职' },
  { id: '003', name: '王五', role: '营业员', phone: '13700137000', entryDate: '2022-05-10', status: '在职' },
  { id: '004', name: '赵六', role: '仓管', phone: '13600136000', entryDate: '2022-06-05', status: '在职' },
  { id: '005', name: '钱七', role: '营业员', phone: '13500135000', entryDate: '2022-07-15', status: '离职' }
])

// 员工表单
const showAddStaffDialog = ref(false)
const staffDialogTitle = ref('新增员工')
const staffForm = reactive({
  id: '',
  name: '',
  role: '',
  phone: '',
  entryDate: '',
  status: '在职'
})

// 编辑员工
const editStaff = (staff) => {
  staffDialogTitle.value = '编辑员工'
  Object.assign(staffForm, staff)
  showAddStaffDialog.value = true
}

// 保存员工
const saveStaff = () => {
  if (staffForm.id) {
    // 编辑现有员工
    const index = staffList.value.findIndex(s => s.id === staffForm.id)
    if (index !== -1) {
      staffList.value[index] = { ...staffForm }
    }
  } else {
    // 添加新员工
    const newId = String(Number(staffList.value[staffList.value.length - 1].id) + 1).padStart(3, '0')
    staffList.value.push({
      ...staffForm,
      id: newId
    })
  }
  
  showAddStaffDialog.value = false
  ElMessage.success(staffForm.id ? '员工信息已更新' : '新员工已添加')
  
  // 重置表单
  Object.keys(staffForm).forEach(key => {
    staffForm[key] = key === 'status' ? '在职' : ''
  })
}

// 确认删除员工
const confirmDeleteStaff = (staff) => {
  ElMessageBox.confirm(
    `确定要删除员工"${staff.name}"吗？`,
    '删除员工',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    staffList.value = staffList.value.filter(s => s.id !== staff.id)
    ElMessage.success('员工已删除')
  }).catch(() => {
    // 取消操作
  })
}

// 会员数据
const memberList = ref([
  { id: 'M001', name: '张顾客', phone: '13812345678', level: '金卡会员', points: 2580, joinDate: '2023-01-10', lastVisit: '2023-05-15' },
  { id: 'M002', name: '李顾客', phone: '13912345678', level: '普通会员', points: 680, joinDate: '2023-02-18', lastVisit: '2023-05-12' },
  { id: 'M003', name: '王顾客', phone: '13712345678', level: '银卡会员', points: 1290, joinDate: '2023-03-05', lastVisit: '2023-05-10' },
  { id: 'M004', name: '赵顾客', phone: '13612345678', level: '钻石会员', points: 5680, joinDate: '2023-01-20', lastVisit: '2023-05-18' },
  { id: 'M005', name: '钱顾客', phone: '13512345678', level: '普通会员', points: 350, joinDate: '2023-04-15', lastVisit: '2023-05-05' }
])

// 会员表单
const showAddMemberDialog = ref(false)
const memberDialogTitle = ref('新增会员')
const memberForm = reactive({
  id: '',
  name: '',
  phone: '',
  level: '普通会员',
  points: 0,
  joinDate: new Date().toISOString().split('T')[0],
  lastVisit: ''
})

// 编辑会员
const editMember = (member) => {
  memberDialogTitle.value = '编辑会员'
  Object.assign(memberForm, member)
  showAddMemberDialog.value = true
}

// 会员详情
const viewMemberDetails = (member) => {
  ElMessage.info(`会员详情: ${member.name}，积分: ${member.points}`)
  // 这里可以实现会员详情页面跳转或模态框
}

// 保存会员
const saveMember = () => {
  if (memberForm.id) {
    // 编辑现有会员
    const index = memberList.value.findIndex(m => m.id === memberForm.id)
    if (index !== -1) {
      memberList.value[index] = { ...memberForm }
    }
  } else {
    // 添加新会员
    const newId = `M${String(memberList.value.length + 1).padStart(3, '0')}`
    memberList.value.push({
      ...memberForm,
      id: newId,
      joinDate: new Date().toISOString().split('T')[0],
      lastVisit: new Date().toISOString().split('T')[0]
    })
  }
  
  showAddMemberDialog.value = false
  ElMessage.success(memberForm.id ? '会员信息已更新' : '新会员已添加')
  
  // 重置表单
  Object.keys(memberForm).forEach(key => {
    if (key === 'level') {
      memberForm[key] = '普通会员'
    } else if (key === 'points') {
      memberForm[key] = 0
    } else if (key === 'joinDate') {
      memberForm[key] = new Date().toISOString().split('T')[0]
    } else {
      memberForm[key] = ''
    }
  })
}

// 获取会员等级类型
const getMemberLevelType = (level) => {
  switch(level) {
    case '钻石会员': return 'danger'
    case '金卡会员': return 'warning'
    case '银卡会员': return 'info'
    default: return 'success'
  }
}

// 最近订单数据
const recentOrders = ref([
  { name: '张顾客', orderId: '2305190001', time: '10分钟前', items: 3, amount: '298.00', avatar: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png' },
  { name: '王顾客', orderId: '2305190002', time: '25分钟前', items: 2, amount: '159.00', avatar: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png' },
  { name: '李顾客', orderId: '2305190003', time: '40分钟前', items: 5, amount: '582.00', avatar: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png' },
  { name: '赵顾客', orderId: '2305190004', time: '1小时前', items: 1, amount: '99.00', avatar: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png' }
])
</script>

<style scoped lang="scss">
.management-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100%;
  background-color: #f5f7fa;
  color: #333;
  font-family: Arial, sans-serif;
}

.management-header {
  display: flex;
  align-items: center;
  background-color: #1e4b9a;
  color: white;
  padding: 0.5rem 1rem;
  height: 60px;

  .logo {
    font-size: 1.2rem;
    font-weight: bold;
    margin-right: 1rem;
  }

  .store-info {
    flex: 1;
    .store-code {
      background-color: white;
      color: #333;
      padding: 0 0.5rem;
      border-radius: 4px;
      margin-left: 0.5rem;
    }
  }

  .header-actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;

    .header-icons {
      display: flex;
      gap: 0.5rem;
      margin-left: 1rem;
    }
  }
}

.management-content {
  flex: 1;
  padding: 1rem;
  overflow: auto;
  
  .management-menu {
    height: calc(100vh - 90px);
    border-right: none;
  }
  
  .content-area {
    padding: 1rem;
    background-color: white;
    border-radius: 4px;
    min-height: calc(100vh - 100px);
    
    h2 {
      margin-top: 0;
      margin-bottom: 20px;
      color: #1e4b9a;
    }
  }
}

.stat-cards {
  margin-bottom: 20px;
  
  .stat-card {
    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    
    .stat-value {
      font-size: 24px;
      font-weight: bold;
      margin: 10px 0;
    }
    
    .stat-compare {
      font-size: 14px;
      color: #909399;
      
      .up {
        color: #67c23a;
      }
      
      .down {
        color: #f56c6c;
      }
    }
  }
}

.chart-placeholder {
  height: 300px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f7fa;
  color: #909399;
  font-size: 16px;
  border-radius: 4px;
}

.recent-orders {
  .order-info {
    .order-title {
      font-weight: bold;
    }
    
    .order-meta {
      font-size: 12px;
      color: #909399;
    }
  }
  
  .order-amount {
    font-weight: bold;
  }
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
</style> 